<template>
  <div>
    <el-tooltip effect="dark" :content="$t('common.switch_language')" placement="left">
      <el-dropdown trigger="click" @command="changeLanguage">
        <SvgIcon :name="props.textname" />

        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item
              v-for="item in languageList"
              :key="item.value"
              :command="item.value"
              :disabled="locale === item.value"
            >
              {{ item.label }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </el-tooltip>
  </div>
</template>

<script setup>
import { storeToRefs } from 'pinia'
import { useLocaleStore, usePermissionStore } from '@/store'
import { $t } from '@/languages/index'

const localeStore = useLocaleStore()
const permissionStore = usePermissionStore()
const { locale } = storeToRefs(localeStore)

const props = defineProps({
  textname: {
    type: String,
    default: 'language'
  }
})

const languageList = [
  { label: '简体中文', value: 'zh' },
  { label: 'English', value: 'en' }
]

const changeLanguage = lang => {
  localeStore.setLocaleAction(lang)
  permissionStore.setRouterTitle(permissionStore.routes, $t)
}
</script>
